package com.demo.phone.service;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.demo.phone.domain.Phone;
import com.demo.phone.mapper.PhoneMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * @Description:
 * @author: wangshuai
 * @date: 2018/4/13 8:29
 */
@Service
public class PhoneService extends ServiceImpl<PhoneMapper, Phone> {

    private final PhoneMapper phoneMapper;

    public PhoneService(PhoneMapper phoneMapper) {
        this.phoneMapper = phoneMapper;
    }

    public Integer creatPhone(){
        Phone phone = new Phone();
        phone.setProductName("iphoneX");
        phone.setProductColour("黑色");
        phone.setProductDescription("最新款iphone");
        phone.setProductInventory(15);
        phone.setProductPrice(5000D);
        phone.setProductShippingMethod("顺丰");

        return phoneMapper.insert(phone);
    }
    public Integer updatePhone(Phone phone){
        Assert.notNull(phone.getId(),"id is not be null");
        Phone persist = phoneMapper.selectById(phone.getId());
        persist.setProductPrice(phone.getProductPrice());
        return phoneMapper.insert(persist);
    }

    public Phone getPhoneById(Integer id){
        return  phoneMapper.selectById(id);
    }

    public Page<Phone> selectUserPage(Page<Phone> page) {
        // 不进行 count sql 优化，解决 MP 无法自动优化 SQL 问题
        // page.setOptimizeCountSql(false);
        // 不查询总记录数
        // page.setSearchCount(false);
        return page.setRecords(phoneMapper.selectPhoneList(page));
    }

}
